/**
 * FixedRedBlueColourScheme
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap.colours;

import java.awt.Color;

import com.googlecode.colourmap.ColourSchemeKey;


/**
 * A fixed colour scheme has predefined mappings from data values to colours.
 * In this example, blue is used for data values of <= 200
 * Red is used for data values of >= 400
 * In between those values, the map is interpolated.
 * @author Alan W.F. Boyd
 *
 */
public class FixedRedBlueColourScheme implements IColourScheme
{
	/** The minimum data value */
	private float min = 200;
	
	/** The maximum data value */
	private float max = 400;
	
	/** The key to show the user the mapping between colours and data values */
	private ColourSchemeKey k;
	
	/**
	 * Constructs a new FixedRedBlueColourScheme and initialises the key
	 */
	public FixedRedBlueColourScheme()
	{
		k = new ColourSchemeKey(min, max);
		k.setColourScheme(this);
		k.setVisible(true);
	}
	
	/**
	 * Returns the colour of the outline of the data points on the graph
	 * @return the outline colour of the data points
	 */
	public Color getOutlineColour()
	{
		return Color.black;
	}
	
	/**
	 * Returns the colour of a data value, given the data value
	 * @param f the data value
	 * @return the colour associated with the supplied data value
	 */
	public Color getColour(float f)
	{
		if (f <= min)
			return new Color(0, 0, 255);
		else if (f >= max)
			return new Color(255, 0, 0);
		
		// Find out at what point between the max and min this value lies
		float maxDifference = max - min;
		float difference = f - min;
		int magnitude = (int)(difference*1020/maxDifference);
		
		int col = magnitude / 255;
		int str = magnitude % 255;
		
		if (col == 0)
			return new Color(0, str, 255);
		else if (col == 1)
			return new Color(0, 255, 255-str);
		else if (col == 2)
			return new Color(str, 255, 0);
		else if (col == 3)
			return new Color(255, 255-str, 0);
		
		return new Color(255, 0, 0);
	}

	/**
	 * Resets the maximum data value in a data set
	 * @param f the new maximum data value
	 */
	public void setMax(float f)
	{
		k.repaint();
	}

	/**
	 * Resets the minimum data value in a data set
	 * @param f the new minimum data value
	 */
	public void setMin(float f)
	{
		k.repaint();
	}
}
